<template>
  <div class="app-container">
    <div>
      <el-form
        :model="operatorForm"
        label-position="right"
        label-width="130px"
        ref="operatorForm"
      >
        <el-row>
          <el-col :span="8">
            <el-form-item label="账号:" prop="username">
              <el-input
                v-model="operatorForm.username"
                placeholder="请输入内容"
                size="small"
                style="width: 150px"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="center">
          <el-form-item>
            <el-button
              type="success"
              icon="plus"
              style="width: 80px"
              @click="searchByOperator"
              >筛选</el-button
            >
            <!-- v-if="hasPerm('strategy:auto')" -->
            <el-button
              type="danger"
              icon="plus"
              style="width: 80px"
              @click="resetsearchByOperator"
              >重置</el-button
            >
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <div>
       <el-button
              type="success"
              icon="plus"
              style="margin-bottom:20px"
              @click="addOperator()"
              >新增客服</el-button
            >
       </div>
    <el-table
      :data="operatorTable"
      v-loading.body="listLoading"
      element-loading-text="拼命加载中"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column
        align="center"
        label="id"
        prop="id"
        width="150"
        v-if="false"
      ></el-table-column>
      <el-table-column
        align="center"
        label="账号"
        prop="name"
        width="150"
      ></el-table-column>
      <el-table-column
        align="center"
        label="角色编号"
        prop="role"
      ></el-table-column>
     
      <el-table-column align="center" label="操作" fixed="right" width="150">
        <template slot-scope="scope">
          <el-button
            type="text"
            icon="edit"
            @click="editOperator(scope.row)"
            >编辑</el-button
          >
             <el-button
            type="text"
            icon="edit"
            @click="resetPassOperator(scope.row)"
            >重置密码</el-button
          >
          <!-- v-if="hasPerm('user:update')" -->
           <!-- v-if="scope.row.orderStatus == 'EFFECTIVE'" -->
          <!-- v-if="hasPerm('user:update')" -->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleUserSizeChange"
      @current-change="handleCurrentOperatorChange"
      :current-page="listQuery.pageNum"
      :page-size="listQuery.pageRow"
      :total="totalOperatorCount"
      :page-sizes="[10, 20, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <el-dialog
      :title="dialogOperatorTitle"
      :visible.sync="dialogOperatorVisible"
      style="min-width: 800px"
    >
      <el-form
        :model="operatorDetailForm"
        :rules="operatorDetailFormRules"
        label-position="left"
        style="margin-left: 20px"
        ref="operatorDetailForm"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item
              label="账号:"
              label-width="110px"
              style="width: 300px"
              prop="username"
            >
             <el-input v-model="operatorDetailForm.username" autocomplete="off" />
            </el-form-item>
             <el-form-item
              label="密码:"
              label-width="110px"
              style="width: 300px"
              prop="password"
            >
             <el-input v-model="operatorDetailForm.password" autocomplete="off" />
            </el-form-item>
          </el-col>
        </el-row>
       <el-row type="flex" justify="center">
          <el-form-item>
            <el-button
              type="success"
              icon="plus"
              style="width: 80px"
              @click="saveOperator()"
              :loading="loading"
              >保存</el-button
            >
            <!-- v-if="hasPerm('strategy:auto')" -->
            <el-button
              type="danger"
              icon="plus"
              style="width: 80px"
              @click="close()"
              >取消</el-button
            >
          </el-form-item>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      totalOperatorCount: 0, //分页组件--数据总条数
      operatorTable: [], //表格的数据
      listLoading: false, //数据加载等待动画
      listLoadingDialog: false,
      loading: false,
      listQuery: {
        pageNum: 1, //页码
        pageRow: 10, //每页条数
      },

      activeName: "first",
      dialogStatus: "details",
      dialogOperatorVisible:false,
      dialogOperatorTitle:"",
      operatorForm: {
        username: ""
      },
       operatorDetailForm: {
        username:""
      },
      operatorDetailFormRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入密码" }
        ]

      },
    };
  },
  created() {
    // this.searchByOperator();
  },
  filters: {
    checkStatus: function (status, list) {
      let obj = list.find((item) => item.value === status);
      if (obj) {
        return obj.label ? obj.label : "";
      } else {
        return "";
      }
    },
  },
  methods: {
    searchByOperator() {
      //查询列表
      let url =
        "/admin/query/" + this.listQuery.pageNum + "/" + this.listQuery.pageRow;
      this.listLoading = true;
      let data = {};

      data = {
        username: this.operatorForm.username
      };
      this.api({
        url: url,
        method: "post",
        data: data,
      }).then((data) => {
        this.listLoading = false;
        this.operatorTable = data.data.datas;
        this.totalOperatorCount = data.data.total;
      });
    },
    resetsearchByOperator() {
      this.$refs.operatorForm.resetFields();
    },
     handleOperatorFilter() {
      this.listQuery.pageNum = 1;
      this.searchByOperator();
    },
    handleUserSizeChange(val) {
      //改变每页数量
      this.listQuery.pageRow = val;
      this.handleOperatorFilter();
    },
    handleCurrentOperatorChange(val) {
      //改变页码
      this.listQuery.pageNum = val;
      this.searchByOperator();
    },

    getIndex($index) {
      //表格序号
      return (this.listQuery.pageNum - 1) * this.listQuery.pageRow + $index + 1;
    },
    addOperator(){
     this.dialogOperatorVisible = true
     this.dialogOperatorTitle="新增客服"
    },
    editOperator(row) {
      this.dialogOperatorVisible = true
      this.dialogOperatorTitle="编辑客服"
      this.operatorDetailForm.id = row.id
      this.operatorDetailForm.username = row.username
    },
    resetPassOperator(){

    },
    saveOperator()
    {
        this.$refs.operatorDetailForm.validate(valid => {
            if (valid) {
                this.loading = true;
                let url = "admin/register/";
                let params={
                  roleCode:"CC",
                  username:this.operatorDetailForm.username,
                  password:this.operatorDetailForm.password
                };
                this.api({
                  url: url,
                  method: "post",
                  data: params,
                }).then((data) => {
                    this.loading = false;
                    if(data.errorMessage)
                    {
                       this.$message.info(data.errorMessage);
                    }
                    this.searchByOperator()
                    this.dialogOperatorVisible=false;
                });
            }
        })
     
    },
    close()
    {
       this.dialogOperatorVisible=false;
    },

  },
};
</script>
